import { AnyAction, Reducer } from 'redux';
import { message } from 'antd';
import { EffectsCommandMap } from 'dva';
import router from 'umi/router';
import { fetchValidation } from '@/utils';
import {
    queryBoxManagementDetail,
    queryPostList,
    queryGardenList,
    operSaveOrUpdate,
    queryBluetooth,
    queryFirmware,
    queryPosthouseTariff,
} from './service';

export type Effect = (
    action: AnyAction,
    effects: EffectsCommandMap & { select: <T>(func: (state: {}) => T) => T },
) => void;

export interface ModelType {
    namespace: string;
    state: {};
    effects: {
        fetch: Effect;
        fetchPost: Effect;
        fetchGarden: Effect;
        fetchEdition: Effect;
        saveOrUpdate: Effect;
        fetchPosthouseTariff: Effect;
    };
    reducers: {
        save: Reducer;
        savePost: Reducer;
        saveGarden: Reducer;
        saveEdition: Reducer;
        savePostTariff: Reducer;
        clear: Reducer;
    };
}

const stateInfo = {
    detailInfo: { type: 1 },
    postInfo: {
        count: 0,
        list: [],
    },
    gardenInfo: {
        count: 0,
        list: [],
    },
    bluetooth: [],
    firmware: [],
    postTariffList: [],
};

const Model: ModelType = {
    namespace: 'boxManagementAdd',

    state: stateInfo,

    effects: {
        * fetch({ payload }, { call, put }) {
            const response = yield call(queryBoxManagementDetail, payload);
            const data = fetchValidation(response);
            yield data && put({
                type: 'save',
                payload: response.body,
            });
        },

        * fetchPost({ payload }, { call, put }) {
            const response = yield call(queryPostList, payload);
            const data = fetchValidation(response);
            yield data && put({
                type: 'savePost',
                payload: data.body,
            });
        },

        * fetchGarden({ payload }, { call, put }) {
            const response = yield call(queryGardenList, payload);
            const data = fetchValidation(response);
            yield data && put({
                type: 'saveGarden',
                payload: data.body,
            });
        },

        * fetchEdition({ payload }, { call, put }) {
            const bluetooth = yield call(queryBluetooth);
            const firmware = yield call(queryFirmware, payload);
            yield put({
                type: 'saveEdition',
                payload: { bluetooth: Object.values(bluetooth.body) },
            });
            yield put({
                type: 'saveEdition',
                payload: { firmware: Object.values(firmware.body) },
            });
        },

        * fetchPosthouseTariff({ payload }, { call, put }) {
            const response = yield call(queryPosthouseTariff, payload);
            const data = fetchValidation(response);
            yield data && put({
                type: 'savePostTariff',
                payload: (data.body && data.body.list) || [],
            });
        },

        * saveOrUpdate({ payload }, { call, put }) {
            const response = yield call(operSaveOrUpdate, payload);
            const data = fetchValidation(response);
            if (data) {
                message.success(payload.id ? '修改成功' : '添加成功');
                yield put({
                    type: 'global/setPath',
                });
                router.goBack();
            }
        },
    },

    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                detailInfo: payload,
            };
        },

        savePost(state, { payload }) {
            return {
                ...state,
                postInfo: payload,
            };
        },
        saveGarden(state, { payload }) {
            return {
                ...state,
                gardenInfo: payload,
            };
        },

        saveEdition(state, { payload }) {
            return {
                ...state,
                ...payload,
            };
        },

        savePostTariff(state, { payload }) {
            return ({
                ...state,
                postTariffList: payload,
            });
        },

        clear() {
            return stateInfo;
        },
    },
};

export default Model;
